#!/bin/bash
# Job yaml can send below Environment variables to me
# - branch
# - commit
# - arch
# END ENVIRONMENT VARIABLES

## check-kabi is a script originally copied from red hat linux, which is used to
## check whether kabi compatibility is broken or not against to the kabi
## whitelist. common usage: check-kabi -k Module.kabi_aarch64 -s Module.symvers
## if no output is displayed after the script is executed, then the kabi
## compatibility is maintained according to the kabi whitelist.
## if the kabi changed, the list of the changed symbols will be reported, like:
##
## *** ERROR - ABI BREAKAGE WAS DETECTED ***
## The following symbols have been changed (this will cause an ABI breakage):
## new kabi:
## 0x8d57306b	alloc_pages	vmlinux	EXPORT_SYMBOL
## 0xf1cf914e	ata_link_next	drivers/ata/libata	EXPORT_SYMBOL_GPL
## 0xe26080fb	blk_alloc_queue	vmlinux	EXPORT_SYMBOL
## ......

## license=('Mulan PSL v2')

# LTS branchs mapping from src-openeuler/kernel to openeuler/kernel
declare -A branch_mapping
branch_mapping["openEuler-20.03-LTS"]=openEuler-1.0-LTS
branch_mapping["openEuler-20.03-LTS-SP1"]=openEuler-1.0-LTS
branch_mapping["openEuler-20.03-LTS-SP2"]=openEuler-1.0-LTS
branch_mapping["openEuler-20.03-LTS-SP3"]=openEuler-1.0-LTS
branch_mapping["openEuler-20.03-LTS-SP4"]=openEuler-1.0-LTS
branch_mapping["openEuler-22.03-LTS"]=openEuler-22.03-LTS
branch_mapping["openEuler-22.03-LTS-SP1"]=openEuler-22.03-LTS-SP1
branch_mapping["openEuler-22.03-LTS-SP2"]=openEuler-22.03-LTS-SP2
branch_mapping["openEuler-22.03-LTS-SP3"]=OLK-5.10
branch_mapping["openEuler-22.03-LTS-SP4"]=OLK-5.10
branch_mapping["openEuler-24.03-LTS"]=OLK-6.6
branch_mapping["openEuler-24.03-LTS-SP1"]=OLK-6.6

openeuler_kernel_url="https://gitee.com/openeuler/kernel.git"
src_openeuler_kernel_url="https://gitee.com/src-openeuler/kernel.git"

check_vars()
{
	[ -n "$branch" ] || die "branch is empty"
	[ -n "$commit" ] || die "commit is empty"
}

clean_workspace()
{
	rm -rf kernel
}

clone_openeuler_kernel()
{
	git clone --single-branch --branch ${branch_mapping["$branch"]} $openeuler_kernel_url
}

build_kernel()
{
	cd kernel
	git reset --hard $commit > build.log
	make openeuler_defconfig >> build.log
	make -j30 >> build.log
}

clone_src_openeuler()
{
	mkdir src_openeuler
	cd src_openeuler
	git clone --depth 1 --single-branch --branch $branch $src_openeuler_kernel_url
}

# generate key:value pairs list from Module.kabi_arch and Module.symvers,
# then compare the two lists to get the difference and output it.
check_kabi()
{
	cd kernel
	chmod +x check-kabi
	# check-kabi script will also sys.exit(1) if exception occured, for example parameter error.
	./check-kabi -k Module.kabi_aarch64 -s ../../Module.symvers
	if [ $? -eq 0 ];then
		echo "Success! No KABI was changed."
	else
		res=$(./check-kabi -k Module.kabi_aarch64 -s ../../Module.symvers)
		if [[ "$res" =~ "ABI BREAKAGE WAS DETECTED" ]];then
			echo "Error! KABI Breakage was detected!"
		else
			echo "Error! Run check-kabi command failed!"
		fi
	fi
}

main()
{
	check_vars
	clean_workspace
	clone_openeuler_kernel || abort
	build_kernel || abort
	clone_src_openeuler || abort
	check_kabi
}

main
